//package developer.zyk.ability.chat.ability.rag.vector;
//
//import developer.zyk.global.common.consts.ChatCommonConstants;
//import developer.zyk.global.user.bo.UserInfo;
//import developer.zyk.global.util.AssertUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.ai.document.Document;
//import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
//import org.springframework.ai.vectorstore.SearchRequest;
//import org.springframework.ai.vectorstore.SimpleVectorStore;
//import org.springframework.ai.vectorstore.SimpleVectorStoreContent;
//import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore;
//import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
//import org.springframework.expression.spel.support.StandardEvaluationContext;
//
//import java.io.File;
//import java.util.*;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.function.Predicate;
//
///**
// * @author zhangyongkang
// * @time 2025/5/16 14:28
// * @description
// */
//@Slf4j
//public class MultipleUserSimpleVector extends AbstractObservationVectorStore implements UserStoreVector {
//
//    protected Map<Long, Map<String, SimpleVectorStoreContent>> store = new ConcurrentHashMap();
//
//    public MultipleUserSimpleVector(AbstractVectorStoreBuilder<?> builder) {
//        super(builder);
//    }
//
//    @Override
//    public void doAdd(List<Document> documents) {
//        if (documents.isEmpty()) {
//            throw new IllegalArgumentException("Documents list cannot be empty");
//        } else {
//            List<Long> userIds = documents.stream()
//                    .filter(Objects::nonNull)
//                    .map(document -> {
//                        log.info("Calling EmbeddingModel for document id = {}", document.getId());
//                        float[] embedding = this.embeddingModel.embed(document);
//                        SimpleVectorStoreContent storeContent = new SimpleVectorStoreContent(document.getId(), document.getText(), document.getMetadata(), embedding);
//                        Long userId = (Long) document.getMetadata().get(ChatCommonConstants.DocumentMetaDataField.USER_ID);
//                        AssertUtil.assertNonNull(userId, "document用户信息为空");
//
//                        Map<String, SimpleVectorStoreContent> currentUserStore = this.store.getOrDefault(userId, new HashMap<>());
//                        currentUserStore.put(document.getId(), storeContent);
//                        this.store.put(userId, currentUserStore);
//                        return userId;
//                    })
//                    .toList();
//        }
//    }
//
//    @Override
//    public void doDelete(List<String> idList) {
//        Optional.of(idList).orElse(new ArrayList<>())
//                .stream()
//                .map(a -> {
//                    String[] split = a.split(ChatCommonConstants.DocumentMetaDataField.MULTIPLE_VECTOR_ID_SPECTOR);
//                    Long userId = Long.valueOf(split[0]);
//                    String documentId = split[1];
//                    store.getOrDefault(userId, new HashMap<>()).remove(documentId);
//                    return userId;
//                });
//    }
//
//    @Override
//    public List<Document> doSimilaritySearch(SearchRequest request) {
//        Predicate<SimpleVectorStoreContent> documentFilterPredicate = this.doFilterPredicate(request);
//        float[] userQueryEmbedding = this.getUserQueryEmbedding(request.getQuery());
//        return this.store.getOrDefault().values().stream().filter(documentFilterPredicate).map((content) -> content.toDocument(SimpleVectorStore.EmbeddingMath.cosineSimilarity(userQueryEmbedding, content.getEmbedding()))).filter((document) -> document.getScore() >= request.getSimilarityThreshold()).sorted(Comparator.comparing(Document::getScore).reversed()).limit((long) request.getTopK()).toList();
//    }
//
//    private Predicate<SimpleVectorStoreContent> doFilterPredicate(SearchRequest request) {
//        return request.hasFilterExpression() ? (document) -> {
//            StandardEvaluationContext context = new StandardEvaluationContext();
//            context.setVariable("metadata", document.getMetadata());
//            return (Boolean) this.expressionParser.parseExpression(this.filterExpressionConverter.convertExpression(request.getFilterExpression())).getValue(context, Boolean.class);
//        } : (document) -> true;
//    }
//
//    private float[] getUserQueryEmbedding(String query) {
//        return this.embeddingModel.embed(query);
//    }
//
//    @Override
//    public VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName) {
//        return null;
//    }
//
//
//    @Override
//    public void save(File file, UserInfo userInfo) {
//
//    }
//
//    @Override
//    public void load(File file, UserInfo userInfo) {
//
//    }
//}
